package com.pipi.biyesheji.service.impl;

import com.pipi.biyesheji.dao.manager.PermissionManager;
import com.pipi.biyesheji.dao.manager.RolePermissionManager;
import com.pipi.biyesheji.dao.model.PermissionDO;
import com.pipi.biyesheji.dao.model.RolePermissionDO;
import com.pipi.biyesheji.dao.query.PermissionQuery;
import com.pipi.biyesheji.dao.query.RolePermissionQuery;
import com.pipi.biyesheji.pojo.Permission;
import com.pipi.biyesheji.pojo.Role;
import com.pipi.biyesheji.pojo.RolePermission;
import com.pipi.biyesheji.service.PermissionService;
import com.pipi.biyesheji.service.RoleService;
import com.pipi.biyesheji.service.UserService;
import com.pipi.biyesheji.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author pipi
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionManager permissionManager;
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    private RolePermissionManager rolePermissionManager;

    @Override
    public Set<String> listPermissions(String userName) {
        return getPermissionDOs(userName).stream().map(PermissionDO::getName)
                .filter(Objects::nonNull).collect(Collectors.toSet());
    }



    @Override
    public List<Permission> list(Role role) {
        RolePermissionQuery rolePermissionQuery = new RolePermissionQuery();
        rolePermissionQuery.createCriteria().andRidEqualTo(role.getId());
        List<RolePermissionDO> rolePermissionDOS = rolePermissionManager.selectByQuery(rolePermissionQuery);
        List<Long> permissionIds = rolePermissionDOS.stream().map(RolePermissionDO::getPid).collect(Collectors.toList());
        PermissionQuery permissionQuery = new PermissionQuery();
        permissionQuery.createCriteria().andIdIn(permissionIds);
        List<PermissionDO> permissionDOS = permissionManager.selectByQuery(permissionQuery);
        return buildPermissions(permissionDOS);
    }


    @Override
    public List<Permission> list() {
        PermissionQuery permissionQuery = new PermissionQuery();
        permissionQuery.setOrderByClause("id desc");
        List<PermissionDO> permissionDOS = permissionManager.selectByQuery(permissionQuery);
        return buildPermissions(permissionDOS);
    }

    @Override
    public void add(Permission permission) {
        PermissionDO permissionDO = BeanUtils.createCloneBean(permission, PermissionDO.class);
        permissionManager.insertSelective(permissionDO);
    }

    @Override
    public void delete(Long id) {
        permissionManager.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        PermissionDO permissionDO = BeanUtils.createCloneBean(permission, PermissionDO.class);
        permissionManager.updateByPrimaryKeySelective(permissionDO);
    }

    @Override
    public Permission get(Long id) {
        PermissionDO permissionDO = permissionManager.selectByPrimaryKey(id);
        return buildPermission(permissionDO);
    }


    @Override
    public boolean needInterceptor(String requestURI) {
        List<Permission> ps = list();
        for (Permission p : ps) {
            if (p.getUrl().equals(requestURI)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Set<String> listPermissionURLs(String userName) {
        return getPermissionDOs(userName).stream().map(PermissionDO::getUrl)
                .filter(Objects::nonNull).collect(Collectors.toSet());

    }


    private List<Permission> buildPermissions(List<PermissionDO> permissionDOS) {
       return permissionDOS.stream().map(this::buildPermission).collect(Collectors.toList());
    }

    private Permission buildPermission(PermissionDO permissionDO) {
        if (Objects.isNull(permissionDO)) {
            return null;
        }
        return BeanUtils.createCloneBean(permissionDO, Permission.class);
    }

    private List<RolePermission> buildRolePermissions(List<RolePermissionDO> rolePermissionDOS) {
        return rolePermissionDOS.stream().map(this::buildRolePermission).collect(Collectors.toList());
    }

    private RolePermission buildRolePermission(RolePermissionDO rolePermissionDO) {
        if (Objects.isNull(rolePermissionDO)) {
            return null;
        }
        return BeanUtils.createCloneBean(rolePermissionDO, RolePermission.class);
    }


    private List<PermissionDO> getPermissionDOs(String userName) {
        //获得当前管理员的角色列表
        List<Role> roles = roleService.listRoles(userName);

        List<Long> roleIds = roles.stream().map(Role::getId).collect(Collectors.toList());

        RolePermissionQuery rolePermissionQuery = new RolePermissionQuery();
        rolePermissionQuery.createCriteria().andRidIn(roleIds);
        //保存关联查询的记录（角色id==权限id的记录）
        List<RolePermissionDO> rolePermissionDOS = rolePermissionManager.selectByQuery(rolePermissionQuery);

        List<Long> permissionIds = rolePermissionDOS.stream().map(RolePermissionDO::getPid).collect(Collectors.toList());

        PermissionQuery permissionQuery = new PermissionQuery();
        permissionQuery.createCriteria().andIdIn(permissionIds);
        return permissionManager.selectByQuery(permissionQuery);
    }

}
